<?php

namespace app\api\controller\product;

use app\admin\model\Shop;
use app\api\model\product\Category as CategoryModel;
use app\api\model\product\Product;
use app\api\controller\Controller;
use app\api\model\supplier\Supplier;
use app\common\model\page\PageCategory as PageCategoryModel;
use app\shop\model\product\Product as ProductModel;

/**
 * 商品分类控制器
 */
class Category extends Controller
{
    /**
     * 分类页面
     */
    public function index()
    {
        // 分类模板
        $template = PageCategoryModel::detail();
        // 商品分类列表
        $list = array_values(CategoryModel::getCacheTree());
        return $this->renderSuccess('', compact('template','list'));
    }
    //根据二级分类id查询商品，根据商品查询店铺
    public function getShopList()
    {
        $param = $this->request->param();
        $list = $this->getha();
        return $list;
//        return $this->renderSuccess('',);
    }
    //根据商品查找所有的店铺
    public function getha(){

        $param = $this->request->param();

        if(empty($param['category_id'])){

            return json(['code'=>0,'mgs'=>'商品category_id未传']);
        }

        $shop = new Product();//商品模型对象
//        halt(111);
        //获取所有该分类的商品商家id

        $stores= $shop->where('category_id',$param['category_id'])->column('shop_supplier_id');

        //定义门店模型
        $supplier = new Supplier();
        $res = [];
        foreach ($stores as $k=>$v){

            $data['shop_supplier_id']=$v;
            $res[] =$supplier->getDetail($data,false)->toArray();

        }
        return json(['data'=>$res]);

    }




    //获取店铺所有的商品
    public function getShopall(){

        $param = $this->request->param();

        if(empty($param['shop_supplier_id'])){
            return json(['err'=>'参数有误']);

        }

        //商品模型对象
        $shop = new Product();
        //该店铺的所有商品
        $data = $shop->where('shop_supplier_id',$param['shop_supplier_id'])->select()->toArray();
        //店铺详情
        $supplier = new Supplier();

        $detail = $supplier->getDetail($param,false)->toArray();

        return json(['product'=>$data,'detail'=>$detail]);


    }

    //商品详情
    public function detail(){
        $param  = $this->request->param();

        if(empty($param['product_id'])){

            return json(['err'=>'参数有误']);

        }

        $product_id = $param['product_id'];

        $detail = ProductModel::detail($product_id)->toArray();

        return json(['detail'=>$detail]);

    }
}